﻿#------------------------------------------------------------------
#add_project.cmake
#please dont modify this cmake
#2018年7月11日 17点12分
#------------------------------------------------------------------
#以下TARGET命令适用于CMakeLists.txt里面有一个execute或者library的时候。
#如果有多个需要改进。project name = execute/library name
#获取target的确切的名字
#区分debug和release用的。
#输入，target_name 为空，则默认为TARGET
#输出，target_name?x
#这个变量用来保存原生的target name
SET(TARGET_NAME ${CMAKE_PROJECT_NAME})
SET(TARGET_DECORATE_NAME ${CMAKE_PROJECT_NAME})
#获取TARGET name
#修饰TARGET _d _debug
#保证修饰
#输入：INPUT_TARGET_NAME
#输出：RESULT_NAME
FUNCTION (ADD_DECORATE_TARGET_NAME INPUT_NAME RESULT_NAME)
    SET(TEMP_NAME ${TARGET_NAME} )
    IF(INPUT_NAME)
        SET(TEMP_NAME ${INPUT_NAME})
    ENDIF()

    SET(RET ${TEMP_NAME})
    IF(CMAKE_BUILD_TYPE STREQUAL Debug)
        IF(APPLE)
            SET(RET ${RET}_debug)
        ELSEIF(WIN32)
            SET(RET ${RET}d)
        ENDIF()
    ENDIF()

    SET(${RESULT_NAME} ${RET} PARENT_SCOPE)
ENDFUNCTION()

#保证还原
#输出：RESULT_NAME
FUNCTION (ADD_TARGET_NAME RESULT_NAME)
    SET(${RESULT_NAME} ${TARGET_NAME} PARENT_SCOPE)
ENDFUNCTION()

#修饰
#输入[TARGET_NAME]修饰，不输出，直接修饰
FUNCTION(ADD_DECORATE_TARGET)
    SET(TEMPNAME ${TARGET_NAME})
    IF(ARGV1)
        SET(TEMPNAME ${ARGV1})
    ENDIF()

    ADD_DECORATE_TARGET_NAME(${TEMPNAME}, TEMPNAME)
    SET(${TARGET_DECORATE_NAME} ${TEMPNAME} PARENT_SCOPE)
    PROJECT_NAME(${TEMPNAME})
ENDFUNCTION()

#还原
#不输入，不输出，直接修饰。
FUNCTION(ADD_TARGET)
    SET(${TARGET_DECORATE_NAME} ${TARGET_NAME} PARENT_SCOPE)
    PROJECT_NAME(${TARGET_NAME})
ENDFUNCTION()

FUNCTION( MESSAGE_LIST INPUTS )
    foreach(loop_var ${INPUTS})
        message( ${loop_var} )
    endforeach()
ENDFUNCTION()

FUNCTION( FILTER_OUT INPUTS FILTERS OUTPUTS )
    set( FOUT "" )

    foreach( INP ${INPUTS} )
        set( FILTERED 0 )

        foreach( FILT ${FILTERS} )
            if( ${FILTERED} EQUAL 0 )
                if( "${FILT}" STREQUAL "${INP}" )
                    set( FILTERED 1 )
                endif( "${FILT}" STREQUAL "${INP}" )
                if( ${INP} MATCHES ${FILT} )
                    set( FILTERED 1 )
                endif( ${INP} MATCHES ${FILT} )
            endif( ${FILTERED} EQUAL 0 )
        endforeach()

        if( ${FILTERED} EQUAL 0 )
            set( FOUT ${FOUT} ${INP} )
        endif()
    endforeach( )

    set( ${OUTPUTS} ${FOUT} PARENT_SCOPE)
ENDFUNCTION()

